package com.atguigu.guli.service.edu.controller.admin;


import com.atguigu.guli.service.base.result.R;
import com.atguigu.guli.service.edu.entity.Teacher;
import com.atguigu.guli.service.edu.entity.query.TeacherQuery;
import com.atguigu.guli.service.edu.feign.OssFileService;
import com.atguigu.guli.service.edu.service.TeacherService;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 讲师 前端控制器
 * </p>
 *
 * @author atguigu
 * @since 2021-12-27
 */
@CrossOrigin
@Slf4j
@Api(tags = "讲师管理")
@RestController
@RequestMapping("/admin/edu/teacher")
public class TeacherController {

    @Autowired
    TeacherService teacherService;

    @ApiOperation("所有讲师列表")
    @GetMapping("list")
    public R listAll(){
        List<Teacher> list = teacherService.list();
        return R.ok().data("items", list).message("获取讲师列表成功");
    }

    @ApiOperation(value = "根据ID删除讲师", notes = "根据ID删除讲师")
    @DeleteMapping("removeById/{id}")
    public R removeById(@ApiParam(value = "讲师ID", required = true) @PathVariable String id){
        teacherService.removeAvatarById(id);
        boolean result = teacherService.removeById(id);
        teacherService.setAvatar(id);
        if(result){
            return R.ok().message("删除成功");
        }else{
            return R.error().message("数据不存在");
        }
    }

    @ApiOperation(value = "批量删除讲师", notes = "根据id批量删除讲师")
    @DeleteMapping("batch-remove")
    public R removeRows(@ApiParam(value = "讲师ID列表", required = true) @RequestBody List<String> idList){
        boolean result = teacherService.removeByIds(idList);
        if(result){
            return R.ok().message("删除成功");
        }else{
            return R.error().message("数据不存在");
        }
    }

    @ApiOperation("分页讲师列表")
    @GetMapping("listPage/{page}/{limit}")
    public R listPage(@ApiParam(value = "当前页码",required = true) @PathVariable Long page,
                      @ApiParam(value = "当前页码显示数量",required = true) @PathVariable long limit,
                      @ApiParam("讲师列表查询对象")TeacherQuery teacherQuery){
        IPage<Teacher> page1 = teacherService.selectPage(page,limit,teacherQuery);
        return R.ok().data("page1",page1);
    }

    @ApiOperation("新增讲师")
    @PostMapping("add")
    public R add(@ApiParam(value = "讲师对象",required = true,defaultValue = "1") @RequestBody Teacher teacher){
        boolean result = teacherService.save(teacher);
        if (result) {
            return R.ok().message("添加成功");
        } else {
            return R.error().message("添加失败");
        }
    }

    @ApiOperation("根据Id修改讲师信息")
    @PutMapping("update")
    public R update(@ApiParam(value = "讲师对象",required = true) @RequestBody Teacher teacher){
        boolean result = teacherService.updateById(teacher);
        if (result){
            return R.ok().message("修改成功");
        }else {
            return R.error().message("修改失败");
        }
    }

    @ApiOperation("根据Id查询讲师信息")
    @GetMapping("get/{id}")
    public R getById(@ApiParam(value = "讲师id",required = true) @PathVariable String id){
        Teacher teacher = teacherService.getById(id);
        if (teacher != null) {
            return R.ok().data("item",teacher);
        } else {
            return R.error().message("查询失败");
        }
    }

    @Autowired
    private OssFileService ossFileService;

    @ApiOperation("测试服务调用")
    @GetMapping("test")
    public R test(){
        ossFileService.test();
        return R.ok();
    }

    @ApiOperation("测试message1")
    @GetMapping("/message1")
    public String message1() {
        return "message1";
    }
}

